package com.slf.engine.threads;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.springframework.web.context.WebApplicationContext;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBossService;
import com.slf.engine.services.IHpBossService;
import com.slf.engine.services.impl.BossServiceImpl;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.HpbossServiceImpl;


public class GetRequestThread extends Thread {

	private static final org.apache.log4j.Logger log = Logger.getLogger(GetRequestThread.class);
	
	private WebApplicationContext context;
	public GetRequestThread(WebApplicationContext context)
	{
		log.log(Priority.INFO, "GET_REQUEST_THREAD RUN SUCCESS");
		this.context = context;
	}
	
	public void run()
	{
		IBossService bossService = new BossServiceImpl();
//		IHpBossService bossService = new HpbossServiceImpl();
		while(true)
		{
//			List<Order> list = bossService.getListOrders();
			Order order = bossService.getOneOrder();
			if(order != null)
			{
				IBaseDao baseDao = (IBaseDao) context.getBean("baseDao");
				try {
					baseDao.add(order, "addOrder");
					log.log(Priority.INFO, "交易队列添加订单成功order_id:" + order.getOrderId() + " account:" + order.getAccount() + " money:" + order.getMoney());
					bossService.doPost(order);
					SysContext.ORDER_LIST.add(order);
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						
					}
				} 
			}else{
				try {
					Thread.sleep(3000);
					//log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
		}
	}
}
